#!/bin/sh

. /lib/functions.sh
. /lib/functions/network.sh
. /usr/share/libubox/jshn.sh
. /lib/mwan3/common.sh

IPS="ipset"
IPT4="iptables -t mangle -w"
IPT6="ip6tables -t mangle -w"

report_policies() {
	local ipt="$1"
	local policy="$2"

	local percent total_weight weight iface

	total_weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | head -1 | awk '{print $3}')

	for iface in $($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '{print $1}'); do
		weight=$($ipt -S $policy | grep -v '.*--comment "out .*" .*$' | cut -s -d'"' -f2 | awk '$1 == "'$iface'"' | awk '{print $2}')
		percent=$(($weight*100/$total_weight))
		json_add_object
		json_add_string interface "$iface"
		json_add_int percent "$percent"
		json_close_object
	done
}

report_policies_v4() {
	local policy

	for policy in $($IPT4 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
		json_add_array "${policy##*mwan3_policy_}"
		report_policies "$IPT4" "$policy"
		json_close_array
	done
}

report_policies_v6() {
	local policy

	for policy in $($IPT6 -S | awk '{print $2}' | grep mwan3_policy_ | sort -u); do
		json_add_array "${policy##*mwan3_policy_}"
		report_policies "$IPT6" "$policy"
		json_close_array
	done
}

get_mwan3_status() {
	local ipv
	local iface="${1}"
	local iface_select="${2}"

	if [ "${iface}" != "${iface_select}" ] && [ "${iface_select}" != "" ]; then
		return
	fi

	config_get ipv "$iface" family "any"

	json_add_object "${iface}"
	for family in "ipv4" "ipv6"; do
	[ "$ipv" != "any" ] && [ "$ipv" != "$family" ] && continue
	json_add_object "$family"

	local running="0"
	local age=0
	local online=0
	local offline=0
	local up="0"
	local uptime="0"
	local enabled pid device time_p time_n time_u time_d status

	network_get_device device $1

	if [ "${iface}" = "${iface_select}" ] || [ "${iface_select}" = "" ]; then
		pid="$(pgrep -f "mwan3track $family $iface $device")"
		if [ "${pid}" != "" ]; then
			running="1"
		fi

		age=$(get_age "$iface" "$family")
		online=$(get_online_time "$iface" "$family")
		offline=$(get_offline_time "$iface" "$family")

		config_get enabled "$iface" enabled 0
		network_get_uptime uptime "$iface"
		network_is_up "$iface" && up="1"

		if [ -f "$MWAN3TRACK_STATUS_DIR/${iface}.$family/STATUS" ]; then
			status="$(cat "$MWAN3TRACK_STATUS_DIR/${iface}.$family/STATUS")"
		else
			status="unknown"
		fi

		json_add_int age "$age"
		json_add_int online "${online}"
		json_add_int offline "${offline}"
		json_add_int uptime "${uptime}"
		json_add_int "score" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}.$family/SCORE")"
		json_add_int "lost" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}.$family/LOST")"
		json_add_int "turn" "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}.$family/TURN")"
		json_add_string "status" "${status}"
		json_add_boolean "enabled" "${enabled}"
		json_add_boolean "running" "${running}"
		json_add_boolean "up" "${up}"
		json_add_array "track_ip"
		for file in $(ls $MWAN3TRACK_STATUS_DIR/${iface}.$family/TRACK_*); do
			track="${file#*/TRACK_}"
			json_add_object
			json_add_string ip "${track}"
			json_add_string status "$(cat "${file}")"
			json_add_int latency "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}.$family/LATENCY_${track}" 2>/dev/null)"
			json_add_int packetloss "$(cat "$MWAN3TRACK_STATUS_DIR/${iface}.$family/LOSS_${track}" 2>/dev/null)"
			json_close_object
		done
		json_close_array
	fi

	json_close_object
	done
	json_close_object
}

main () {

	case "$1" in
		list)
			json_init
			json_add_object "status"
			json_add_string "section" "x"
			json_add_string "interface" "x"
			json_add_string "policies" "x"
			json_close_object
			json_dump
			;;
		call)
			case "$2" in
			status)
				local section iface
				read input;
				json_load "$input"
				json_get_var section section
				json_get_var iface interface

				config_load mwan3
				json_init
				case "$section" in
					interfaces)
						json_add_object interfaces
						config_foreach get_mwan3_status interface "${iface}"
						json_close_object
						;;
					policies)
						json_add_object policies
						json_add_object ipv4
						report_policies_v4
						json_close_object
						json_add_object ipv6
						report_policies_v6
						json_close_object
						json_close_object
						;;
					*)
						# interfaces
						json_add_object interfaces
						config_foreach get_mwan3_status interface
						json_close_object
						# policies
						json_add_object policies
						json_add_object ipv4
						report_policies_v4
						json_close_object
						json_add_object ipv6
						report_policies_v6
						json_close_object
						json_close_object
						;;
				esac
				json_dump
				;;
			esac
			;;
	esac
}

main "$@"
